package com.itbaizhan.order.service.impl;

import com.itbaizhan.common.vo.LivegoodsResult;
import com.itbaizhan.order.mapper.OrderMapper;
import com.itbaizhan.order.service.OrderService;
import com.itbaizhan.pojo.Order;
import io.seata.spring.annotation.GlobalTransactional;
import org.apache.dubbo.config.annotation.DubboService;
import org.apache.dubbo.qos.command.impl.Live;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.convert.DurationUnit;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@DubboService
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderMapper orderMapper;

    @Override
    public LivegoodsResult insertOrder(Order order) {
        Long time = System.currentTimeMillis();
        order.setId(time);
        int i = orderMapper.insertOrder(order);
        if(i==1){
            return LivegoodsResult.ok("创建订单完成");
        }
        return LivegoodsResult.error();
    }

    @Override
    public LivegoodsResult showOrder(String user) {
        List<Order> orders = orderMapper.selectOrders(user);
        return LivegoodsResult.ok(orders);
    }

    @Override
    public LivegoodsResult selectOrder(Long id) {
        Order order = orderMapper.selectOrderById(id);
        return LivegoodsResult.ok(order);
    }

    @GlobalTransactional
    @Transactional
    @Override
    public LivegoodsResult updateOrder(Long id, Integer state) {
        int i = orderMapper.updateOrder(id, state);
        if(i==1){
            return LivegoodsResult.ok("成功");
        }
        return LivegoodsResult.error();
    }
}
